/*
 * Cliente.java
 *
 * Created on 4 de Junho de 2007, 21:00
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package servidor;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

/**
 *
 * @author bohrer
 */
public class Cliente extends Thread
{
    private Servidor server;
    private String nickname;
    private Socket sock;
    private DataInputStream inbound;
    private DataOutputStream outbound;
    private boolean ativo;
    private BufferedReader input;
    private BufferedWriter output;
    
    /**
     * Creates a new instance of Cliente
     */
    public Cliente(Socket s,Servidor serv)
    {
        this.sock = s;
        this.server = serv;
        try
        {
            this.outbound = new DataOutputStream( this.sock.getOutputStream( ) ) ;
            this.inbound = new DataInputStream( this.sock.getInputStream( ) ) ;
            this.input = new BufferedReader(new InputStreamReader(this.sock.getInputStream()));
            this.output = new BufferedWriter(new OutputStreamWriter(this.sock.getOutputStream()));
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
        this.ativo = true;
    }
    
    public void send(byte[] b) throws IOException
    {
        this.outbound.write(b,0,100);
    }
    
    public void killClient()
    {
        this.ativo=false;
        this.server.removeClient(this);
        try
        {
            this.outbound.close();
            this.inbound.close();
            this.sock.close() ;
        }
        catch (IOException ex)
        {
        }
    }
    
    public void run()
    {
        try
        {
            int av=0;
            byte[] buffer = new byte[100];
            //Mensagem de boas vindas
            this.output.write("Bem Vindo ao Servidor 666");
            this.output.newLine();
            this.output.write("Utilize LOGIN e PASSWD para se identificar");
            this.output.newLine();
            this.output.flush();
            while (this.ativo)
            {
                String linha = this.input.readLine();
                if (linha!=null)
                {
                    while(linha.indexOf("  ")> -1)
                    {
                        linha = linha.replace("  "," ");
                    }
                    linha = linha.trim();
                    String words[] = linha.split(" ");
                    if (words.length > 0)
                    {
                        if (words[0].toUpperCase().equals("LOCATION"))
                        {
                            if (words.length > 1)
                            {
                                
                            }
                            
                        }
                        else if (words[0].toUpperCase().equals("LOGIN"))
                        {
                            if (words.length > 1)
                            {
                                this.nickname = words[1];
                            }
                        }
                        else if (words[0].toUpperCase().equals("PASSWD"))
                        {
                            if (words.length > 1)
                            {
                                if (this.server.authenticateUser(this.nickname,words[1]))
                                {
                                    this.output.write("Ok\n");
                                }
                                else
                                {
                                    this.output.write("Error\n");
                                }
                            }
                        }
                    }
                }
                this.output.flush();
            }
        }
        catch (IOException ex)
        {
            // ex.printStackTrace();
        }
        finally
        {
            try
            {
                this.ativo=false;
                this.server.removeClient(this);
                this.outbound.close();
                this.inbound.close();
                this.sock.close() ;
            }
            catch (IOException ex)
            {
                ex.printStackTrace();
            }
        }
    }
}


